package ordersubmit

import (
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/logic/promotion/util"
	"mall/service/forum/api/internal/types"
	"mall/service/promotion/rpc/promotionclient"
	// "github.com/zeromicro/go-zero/core/logx"
)

// CouponInfoHandler 获取优惠券信息handler
type CouponInfoHandler struct {
	// 合成复用Next
	Next
}

// Do 获取优惠券信息
func (h *CouponInfoHandler) Do(c *Context) (err error) {
	if c.OrderType == globalkey.OrderTypeVirtual { // 虚拟订单不使用优惠券
		return
	}

	if len(c.Req.CouponIds) == 0 {
		return
	}

	var couponIds []string
	for _, cid := range c.Req.CouponIds {
		if cid > 0 {
			couponIds = append(couponIds, strconv.FormatUint(cid, 10))
		}
	}

	if len(couponIds) == 0 {
		return
	}

	// logx.Infow("获取优惠券信息...")

	var preload string = "coupon:Id,ActivityId,TemplateId,UserId,StartedAt,EndedAt"
	preload += ";couponTemplate:Id,Name,Desc,Members,Mutex,Scope,Mode,Threshhold,DiscountMode,DiscountValue"
	var eagerLoad string = "coupon:couponTemplate"
	var filters []string
	filters = append(filters, "Status,equalTo:"+strconv.FormatInt(int64(globalkey.StatusStarted), 10))
	filters = append(filters, "UserId,equalTo:"+strconv.FormatInt(int64(c.UserId), 10))
	filters = append(filters, "Id,in:"+strings.Join(couponIds, ","))
	res, err := c.SvcCtx.PromotionRpc.GetUserCouponList(c.Ctx, &promotionclient.BaseListReq{
		Preload:    pointy.GetPointer(preload),
		EagerLoad:  pointy.GetPointer(eagerLoad),
		Filter:     pointy.GetPointer(strings.Join(filters, ";")),
		Sorter:     pointy.GetPointer("Id:0"),
		VisitorId:  pointy.GetPointer(c.UserId),
		PlatformId: pointy.GetPointer(globalkey.ThirdPartyPlatformWechatMini),
	})
	if err != nil {
		return err
	}

	if len(res.List) > 0 {
		for _, v := range res.List {
			if v.Rule != nil {
				rule := v.Rule

				mutexTypeText := util.GetMutexTypeText(rule.MutexType)
				scopeTypeText := util.GetScopeTypeText(rule.ScopeType)
				modeText := util.GetCouponModeText(rule.Condition)
				discountModeText := util.GetDiscountModeText(rule.DiscountMode)
				discount := util.GetDiscountValue(rule.DiscountMode, rule.DiscountValue)

				var timeRange []string
				if v.StartedAt != nil && v.EndedAt != nil {
					timeRange = []string{*v.StartedAt, *v.EndedAt}
				}

				c.Resp.CouponList = append(c.Resp.CouponList, &types.CouponInfo{
					CouponTemplateInfo: types.CouponTemplateInfo{
						Id:                rule.Id,
						Name:              rule.Name,
						Description:       rule.Desc,
						MemberRestriction: rule.MemberRestriction,
						MutexType:         rule.MutexType,
						MutexTypeText:     &mutexTypeText,
						ScopeType:         rule.ScopeType,
						ScopeTypeText:     &scopeTypeText,
						Mode:              rule.Condition,
						ModeText:          &modeText,
						Threshold:         rule.ConditionValue,
						DiscountMode:      rule.DiscountMode,
						DiscountModeText:  &discountModeText,
						DiscountValue:     rule.DiscountValue,
						Discount:          &discount,
					},
					CouponId:   v.Id,
					TemplateId: rule.Id,
					ActivityId: v.ActivityId,
					TimeRange:  timeRange,
				})
			}
		}
	}

	return
}
